﻿using Client.Processes;
using Microsoft.Win32;
using Client.ViewModels;

namespace Client.Commands
{
    public class LoadImageFromFileCommand : BaseCommand
    {
        private LoadBytesFromFile loadImage;
        private OpenFileDialog dlg;

        public LoadImageFromFileCommand()
        {
            this.loadImage = new LoadBytesFromFile();
            this.loadImage.OnResult += (bytes) => ImageLoaded(bytes);

            dlg = new OpenFileDialog()
            {
                Filter = "(*.jpg)|*.jpg|(*.jpeg)|*.jpeg",
                Title = "Bildquelle für Cover auswählen",
                Multiselect = false,
                CheckFileExists = true,
                CheckPathExists = true
            };
        }

        public override void Execute(object parameter)
        {
            var result = dlg.ShowDialog();

            if ((bool)result)
                loadImage.Process(dlg.FileName);
        }

        /// <summary>
        /// Daten des aktuellen MovieItem überschreiben
        /// </summary>
        private void ImageLoaded(byte[] bytes)
        {
            var movie = MainViewModel.Default.ActiveWorkspace.MovieItem;
            movie.Cover = bytes;

            MainViewModel.Default.ActiveWorkspace.MovieItem = movie;
        }
    }
}
